package com.yybray.collector;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * Created by Bray.ye on 2017/04/06 10:42.
 */
public class ToListCollector<T> implements Collector<T, List<T>, List<T>> {

    @Override
    public Supplier<List<T>> supplier() {
        //创建一个容器
        //return ArrayList::new;
        return ()->new ArrayList<T>();
    }

    @Override
    public BiConsumer<List<T>, T> accumulator() {
        //将元素放进容器
        //return List::add;
        //return (List<T> list, T item) -> list.add(item);
        return (list, item) -> list.add(item);
    }

    @Override
    public BinaryOperator<List<T>> combiner() {
        return (list1, list2)-> {list1.addAll(list2); return list1;};
    }

    @Override
    public Function<List<T>, List<T>> finisher() {
        return Function.identity();
    }

    @Override
    public Set<Characteristics> characteristics() {
        return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.CONCURRENT));
    }
}
